<template>
<div class="chargebackReason">
  <search-block>
    <template slot="button">
      <el-button type="primary" @click="search">查询</el-button>
      <el-button type="primary" @click="addReason">添加原因</el-button>
    </template>
    <el-form inline :model="model" label-width="100px" @submit.native.prevent>
      <el-form-item label="退单原因:">
        <el-input v-model="model.name" clearable></el-input>
      </el-form-item>
    </el-form>
  </search-block>
  <y-table
    ref="table"
    border
    pagination
    :getDataWay="getDataWay"
    :columns="columns">
    <el-table-column slot="status" label="启用" align="center" header-align="center">
      <template v-slot="scope">
        <el-popconfirm
          :title="scope.row.status === 0 ? '是否确认启用该原因？' : '是否确认停用该原因？'"
          @confirm="() => {
            updateStatus(scope.row)
          }">
          <el-switch
            disabled
            slot="reference"
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"></el-switch>
        </el-popconfirm>
      </template>
    </el-table-column>
    <el-table-column slot="y-oper" label="操作" align="center" header-align="center" width="150">
      <template v-slot="scope">
        <div class="table-buttons">
          <el-button type="primary" size="mini" @click="editReason(scope.row)">编辑</el-button>
          <el-popconfirm
              title="是否确认删除该原因？"
              @confirm="delReason(scope.row)">
            <el-button slot="reference" type="danger" size="mini">删除</el-button>
          </el-popconfirm>
        </div>
      </template>
    </el-table-column>
  </y-table>
  <y-form
    v-model="forms.formModel"
    :visible.sync="forms.showForm"
    :title="forms.formTypeTitle[forms.formsType]"
    destroy-on-close
    width="50%"
    :form-attrs="forms.formAttrs"
    :columns="forms.formColumns"
    @ensure="ensureModel">
  </y-form>
</div>
</template>

<script>
export default {
  name: "chargebackReason",
  computed:{
    getDataWay(){
      return{
        dataType: 'url',
        data: this.$api.goods.chargeBackReasonList,
        param: this.model
      }
    }
  },
  data(){
    return {
      model:{
        name: ''
      },
      columns: [{
        label: '退单原因',
        prop: 'name'
      },{
        label: '排序',
        prop: 'sort'
      },{
        label: '启用',
        prop: 'status',
        template: true
      }],
      forms: {
        formsType: '',
        formTypeTitle: {
          'ADD': '添加原因',
          'EDIT': '编辑原因'
        },
        formModel: {
          name: '',
          sort: ''
        },
        currentForm: null,
        showForm: false,
        formAttrs: {
          labelWidth: '100px',
          rules: {
            name: [{required: true, message: "请输入退单原因", trigger: "blur"}]
          }
        },
        formColumns: [
          {
            label: "退单原因",
            prop: "name",
            comp: {
              compType: 'input'
            }
          },
          {
            label: "排序",
            prop: "sort",
            comp:{
              compType: 'input-number',
              attrs:{
                stepStrictly: true
              }
            }
          }
        ]
      }
    }
  },
  methods:{
    resetForm(){
      Object.keys(this.forms.formModel).map(key => {
        this.forms.formModel[key] = this.forms.currentForm ? this.forms.currentForm[key] : '';
      })
    },
    search(){ this.$refs['table'].loadData(this.model); },
    addReason(){
      this.forms.formsType = 'ADD';
      this.forms.currentForm = null;
      this.resetForm();
      this.forms.showForm = true;
    },
    editReason(row){
      this.forms.formsType = 'EDIT';
      this.forms.currentForm = row;
      this.resetForm();
      this.forms.showForm = true;
    },
    delReason(row){
      this.$yAxios({
        url: this.$api.goods.removeChargeBackReason,
        data:{
          ids: [row.id]
        }
      }, info => {
        this.$message({
          type: 'success',
          message: '删除成功'
        });
        this.search();
      })
    },
    updateStatus(row){
      this.$yAxios({
        url: this.$api.goods.chargeBackReasonStatus,
        data:{
          id: row.id,
          status: row.status ^ 1
        },
        loading: true
      }, info => {
        this.$message({
          type: 'success',
          message: '修改成功'
        });
        this.search();
      });
    },
    ensureModel(form){
      let newForm;
      if(this.forms.formsType === 'EDIT'){
        newForm = Object.assign({}, this.forms.currentForm, form);
      }else{
        newForm = Object.assign({ status: 1 }, form);
      }
      this.$yAxios({
        url: this.$api.goods.saveChargeBackReason,
        data: newForm,
        loading: true
      }, info => {
        this.$message({
          type: 'success',
          message: '保存成功'
        });
        this.search();
      })
    }
  }
}
</script>

<style scoped>

</style>